package com.example.sample.slice;

import com.example.sample.ResourceTable;
import com.ohos.volley.RequestQueue;
import com.ohos.volley.Response;
import com.ohos.volley.VolleyError;
import com.ohos.volley.ext.LocalHiLog;
import com.ohos.volley.toolbox.StringRequest;
import com.ohos.volley.toolbox.Volley;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.HashMap;
import java.util.Map;

public class TestVolleyGetAbilitySlice extends AbilitySlice {

    private Text mResult;

    private static final String GET_URL = "https://gitlab.huawei.com/api/v4/snippets";
    private static final String GET_URL_WITH_HEADER = "https://gitlab.huawei.com/api/v4/snippets";


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_test_volley_get);

        mResult = (Text) findComponentById(ResourceTable.Id_test_get_result);
        TextField urlField = (TextField) findComponentById(ResourceTable.Id_test_volley_get_url);
        urlField.setText(GET_URL);
        RadioContainer radioContainer = (RadioContainer) findComponentById(ResourceTable.Id_test_radio_container);

        Button testBtn = (Button) findComponentById(ResourceTable.Id_test_request_get);
        testBtn.setClickedListener((component -> {
            String url = urlField.getText();
            final int markedId = radioContainer.getMarkedButtonId();
            if(markedId == ResourceTable.Id_with_header) {
                testVolleyWithHeader(url);
            } else if(markedId == ResourceTable.Id_with_parameter) {
                testVolleyWithHeader(url);
            } else {
                testVolley(url);
            }
        }));
    }

    private void testVolley(String url) {
        mResult.setText("开始发起请求……"); // PRIVATE-TOKEN
        RequestQueue mQueue = Volley.newRequestQueue(TestVolleyGetAbilitySlice.this);

        StringRequest stringRequest = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        LocalHiLog.info("TAG", response);
                        mResult.setText(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                LocalHiLog.error("TAG", error.getMessage(), error);
                mResult.setText("请求失败 " + error.toString());
            }
        });

        mQueue.add(stringRequest);
    }

    private void testVolleyWithHeader(String url) {
        mResult.setText("开始发起请求……");
        RequestQueue mQueue = Volley.newRequestQueue(TestVolleyGetAbilitySlice.this);

        StringRequest stringRequest = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        LocalHiLog.info("TAG", response);
                        mResult.setText(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                LocalHiLog.error("TAG", error.getMessage(), error);
                mResult.setText("请求失败 " + error.toString());
            }
        });
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Private-Token", "38jGNz3RSAShGdcRyeSa");
        stringRequest.addHeaders(headers);

        mQueue.add(stringRequest);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
